//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.jim.server.command.handler;

import java.util.Objects;
import org.jim.core.ImChannelContext;
import org.jim.core.ImPacket;
import org.jim.core.config.ImConfig.Global;
import org.jim.core.exception.ImException;
import org.jim.core.packets.ChatBody;
import org.jim.core.packets.ChatType;
import org.jim.core.packets.Command;
import org.jim.core.packets.RespBody;
import org.jim.server.ImServerChannelContext;
import org.jim.server.JimServerAPI;
import org.jim.server.command.AbstractCmdHandler;
import org.jim.server.config.ImServerConfig;
import org.jim.server.protocol.ProtocolManager.Packet;
import org.jim.server.queue.MsgQueueRunnable;
import org.jim.server.util.ChatKit;

public class ChatReqHandler extends AbstractCmdHandler {
	public ChatReqHandler() {
	}

	public ImPacket handler(ImPacket packet, ImChannelContext channelContext) throws ImException {
		ImServerChannelContext imServerChannelContext = (ImServerChannelContext)channelContext;
		if (packet.getBody() == null) {
			throw new ImException("body is null");
		} else {
			ChatBody chatBody = ChatKit.toChatBody(packet.getBody(), channelContext);
			packet.setBody(chatBody.toByte());
			if (chatBody != null && ChatType.forNumber(chatBody.getChatType()) != null) {
				MsgQueueRunnable msgQueueRunnable = this.getMsgQueueRunnable(imServerChannelContext);
				msgQueueRunnable.addMsg(chatBody);
				msgQueueRunnable.executor.execute(msgQueueRunnable);
				ImPacket chatPacket = new ImPacket(Command.COMMAND_CHAT_REQ, (new RespBody(Command.COMMAND_CHAT_REQ, chatBody)).toByte());
				chatPacket.setSynSeq(packet.getSynSeq());
				ImServerConfig imServerConfig = (ImServerConfig)Global.get();
				boolean isStore = ImServerConfig.ON.equals(imServerConfig.getIsStore());
				String groupId;
				if (ChatType.CHAT_TYPE_PRIVATE.getNumber() == chatBody.getChatType()) {
					groupId = chatBody.getToId();
					if (ChatKit.isOnline(groupId, isStore)) {
						JimServerAPI.sendToUser(groupId, chatPacket);
						return Packet.success(chatBody, channelContext);
					} else {
						return Packet.offline(chatBody, channelContext);
					}
				} else if (ChatType.CHAT_TYPE_PUBLIC.getNumber() == chatBody.getChatType()) {
					groupId = chatBody.getGroupId();
					String fromId = chatBody.getFromId();
					JimServerAPI.sendToGroup(groupId, chatPacket, fromId);
					return Packet.success(chatBody, channelContext);
				} else {
					return null;
				}
			} else {
				ImPacket respChatPacket = Packet.dataInCorrect(channelContext);
				return respChatPacket;
			}
		}
	}

	public Command command() {
		return Command.COMMAND_CHAT_REQ;
	}

	private MsgQueueRunnable getMsgQueueRunnable(ImServerChannelContext imServerChannelContext) {
		MsgQueueRunnable msgQueueRunnable = (MsgQueueRunnable)imServerChannelContext.getMsgQue();
		if (Objects.nonNull(msgQueueRunnable.getProtocolCmdProcessor())) {
			return msgQueueRunnable;
		} else {
			Class var3 = MsgQueueRunnable.class;
			synchronized(MsgQueueRunnable.class) {
				msgQueueRunnable.setProtocolCmdProcessor(this.getSingleProcessor());
				return msgQueueRunnable;
			}
		}
	}
}
